//
//  ViewController.m
//  Control Fun
//
//  Created by Mc on 17.03.12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//


#import "ViewController.h"sliderChanged: method:

@interface ViewController ()

@end

@implementation ViewController
@synthesize nameField;
@synthesize numberField;
@synthesize sliderLabel;
@synthesize leftSwitch;
@synthesize rightSwitch;
@synthesize doSomethingButton;

-(IBAction)toggleControls:(id)sender {
    if ([sender selectedSegmentIndex] == kSwitchesSegmentIndex)
    {
        leftSwitch.hidden=NO;
        rightSwitch.hidden=NO;
        doSomethingButton.hidden=YES;
    }
    else {
        leftSwitch.hidden=YES;
        rightSwitch.hidden=YES;
        doSomethingButton.hidden=NO;
    }
}
-(IBAction)switchChanges:(id)sender {
    UISwitch *whichSwitch= (UISwitch *)sender; //Wie>
    BOOL setting = whichSwitch.isOn;
    [leftSwitch setOn:setting animated:YES];
    [rightSwitch setOn:setting animated:YES]; /* Вот тут хотелось бы понять всё таки. Объекту райтсвитч, 
                                               свойству сетОн передается параметр, и анимэтеду тоже? Двум свойствам одновременно чтоли? */
        
}
-(IBAction)buttonPressed{
   UIActionSheet *actionSheet = [[UIActionSheet alloc]
                                 initWithTitle:@"Вы уверены?"
                                 delegate:self cancelButtonTitle:@"Неа!"
                                 destructiveButtonTitle:@"Да, я уверен!" 
                                 otherButtonTitles:nil];
    [actionSheet showInView:self.view];
//    [actionSheet release];
}

-(void) actionSheet:(UIActionSheet *)actionSheet
didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (buttonIndex!=[actionSheet cancelButtonIndex])
    {
        NSString *msg=nil;
        if (nameField.text.length>0)
            msg=[[NSString alloc] initWithFormat:@"Сообщение: %@, отправлено на номер: %@",
                 nameField.text,numberField.text];
        else 
            msg=@"Вы не ввели текст сообщения.";
        UIAlertView *alert = [[UIAlertView alloc]
        initWithTitle:@"Отчет"
        message:msg delegate:self
        cancelButtonTitle:@"Ок!"
        otherButtonTitles:nil];
        [alert show];
//        [alert release];
//        [msg release];
        
    }
}



-(IBAction)sliderChanged:(id)sender {
    UISlider *slider = (UISlider*)sender;
    int progressAsInt = (int)(slider.value+0.5f);  
    NSString *newText = [[NSString alloc] initWithFormat:@"%d",
                         progressAsInt];    /// Как вот это понять?
    sliderLabel.text=newText;                        
//    [newText release];
}

-(IBAction)textFieldDoneEditing:(id)sender {
    [sender resignFirstResponder];
}

-(IBAction)backgroundTap:(id)sender {
    [nameField resignFirstResponder];
    [numberField resignFirstResponder];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	UIImage *buttonImageNormal = [UIImage imageNamed:@"white.jpg"];
    UIImage *stretchablebuttonImageNormal = [buttonImageNormal
                                            stretchableImageWithLeftCapWidth:12 topCapHeight:0];
    [doSomethingButton setBackgroundImage:stretchablebuttonImageNormal forState:UIControlStateNormal];
    
    UIImage *buttonImagePressed = [UIImage imageNamed:@"blue.jpg"];
    UIImage *sretchableButtonImagePressed = [buttonImagePressed
                                             stretchableImageWithLeftCapWidth:12 topCapHeight:0];
    [doSomethingButton setBackgroundImage:sretchableButtonImagePressed forState:UIControlStateHighlighted];
    

}

- (void)viewDidUnload
{
    [super viewDidUnload];
    self.nameField=nil;
    self.numberField=nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (void) dealloc {
//    [nameField release];  
//    [numberField release];
//    [sliderLabel release];
//    [leftSwitch release];
//    [rightSwitch release];
//    [doSomethingButton release];
    
//  [super release];
}

@end
